package com.parktech.sun.web.controller.management.industryinfo;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.model.industryinfo.IndustryInfoCategory;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.industryinfo.IndustryInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;

/**
 * User: Think
 * Date: 11-1-13
 * Time: 下午9:07
 */
@Controller
@RequestMapping("/management/industryinfo/categories")
public class IndustryInfoCategoryController {

    private int pageSize = Constants.PAGINATION_PAGE_SIZE;

    private IndustryInfoService industryInfoService;

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Autowired
    public IndustryInfoCategoryController(IndustryInfoService industryInfoService) {
        this.industryInfoService = industryInfoService;
    }

    @RequestMapping(value = "/search", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView search(IndustryInfoSearchForm industryInfoSearchForm) {

        Page page = new Page(pageSize, industryInfoSearchForm.getPageNo());

        ModelAndView modelAndView = new ModelAndView("management/industryinfo/industryInfoCategorySearch");

        return modelAndView.addObject(industryInfoService.searchCategory(industryInfoSearchForm.getTitle(), page));
    }

    @RequestMapping(value = "/search", method = RequestMethod.DELETE)
    public String delete(@RequestParam("industryInfoCategoryIds") Long[] industryInfoIds) {
        industryInfoService.deleteAll(IndustryInfoCategory.class, industryInfoIds);
        return "redirect:search";
    }

/*
    @ModelAttribute("industryInfoCategory")
    public IndustryInfoCategory populateModel(HttpServletRequest request) {
        String strId = request.getParameter("id");

        if (strId == null || strId.equals("-1")) {
            return new IndustryInfoCategory();
        }
        return industryInfoService.getByCategoryId(new Long(strId));
    }
*/

    @RequestMapping(value = "/{industryInfoCategoryId}", method = RequestMethod.GET)
    public String edit(@PathVariable Long industryInfoCategoryId, Model model) {
        IndustryInfoCategory industryInfoCategory = industryInfoService.getByCategoryId(industryInfoCategoryId);
        model.addAttribute(industryInfoCategory);
        return "management/industryinfo/industryInfoCategoryForm";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String create(Model model) {
        model.addAttribute(new IndustryInfoCategory());
        return "management/industryinfo/industryInfoCategoryForm";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String save(@Valid IndustryInfoCategory industryInfoCategory, BindingResult result) throws IOException {

        if (result.hasErrors()) {
            return "management/industryinfo/industryInfoCategoryForm";
        }
        industryInfoService.saveOrUpdateCategory(industryInfoCategory);

        return "redirect:/management/industryinfo/categories/search";
    }
}
